package com.nstar.sinaweibo.components
{
	
	import com.nstar.base.util.SystemUtils;
	import com.nstar.sinaweibo.controller.MainController;
	import com.nstar.sinaweibo.itemrenderer.UserAvatarThumb;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.core.IFactory;
	import mx.events.CollectionEvent;

	public class CustomListAvatar extends Canvas
	{
		private var _dataProvider:ArrayCollection;
		private var _controller:MainController;	
		private var _itemRenderer:IFactory
		private var ifNeedUpdate:Boolean = false;
		
		private var container:Canvas;
		
		private var _columnCount:int = 5;
		private var _rowCount:int = 5;
		
		public function CustomListAvatar()
		{
			super();
			verticalScrollPolicy = "on";
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			container = new Canvas();
			container.setStyle("backgroundColor", 0xFFFFFF);
			container.percentWidth = 100;
			container.percentHeight = 100;
			addChild(container);
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			if(ifNeedUpdate)
			{
				update();
			}
		}
		
		public function set controller(value:MainController):void
		{
			_controller = value;
			if(!_controller)
			{
				return;
			}
			BindingUtils.bindProperty(this, "dataProvider", _controller.mainData, "mainList");
		}
		
		public function set itemRenderer(value:IFactory):void
		{
			_itemRenderer = value;
		}
		
		public function set dataProvider(value:ArrayCollection):void
		{
			if(_dataProvider)
			{
				_dataProvider.removeEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChange);
			}
			_dataProvider = value;
			if(_dataProvider)
			{
				_dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChange);
			}
			
			needUpdate();
		}
		
		private function onCollectionChange(event:CollectionEvent):void
		{
			needUpdate();
		}
		
		private function needUpdate():void
		{
			ifNeedUpdate = true;
			invalidateProperties();
		}
		
		private function update():void
		{
			if(!container)
			{
				return;
			}
			container.removeAllChildren();
			for each(var data:Object in _dataProvider)
			{
				var obj:Object = _itemRenderer.newInstance();
				obj.controller = _controller;
				obj.data = data;
				this.addEventListener(MouseEvent.MOUSE_DOWN,startDragHandler);
				obj.addEventListener(MouseEvent.MOUSE_OVER,bringToFrontHandler);
				this.addEventListener(MouseEvent.MOUSE_UP,stopDragHandler);
				container.addChild(obj as DisplayObject);
			}
			
			randomLayout();
		}
		
		private function startDragHandler(event:MouseEvent):void{
			if(event.target is UserAvatarThumb){
				event.target.startDrag();
			}
			
		}
		
		private function stopDragHandler(event:MouseEvent):void{
			if(event.target is UserAvatarThumb){
				event.target.stopDrag();
			}
		}

		private function bringToFrontHandler(event:MouseEvent):void{
			if(event.target is UserAvatarThumb){
				container.setChildIndex(event.target as DisplayObject,container.numChildren-1);
			}
		}
		
		//relayout
		private function layout():void{
			var j:int = 0;
			var colCount:int = 0;
			for(var i:int =0;i<container.numChildren;i++){
				var item:DisplayObject = container.getChildAt(i);
				if(colCount==_columnCount){
					colCount = 0;
					j++;
				}
				item.x = colCount*item.width;
				item.y= j*item.height;
				colCount++;
			}
		}
		
		private function randomLayout():void{
			for(var i:int =0;i<container.numChildren;i++){
				var item:DisplayObject = container.getChildAt(i);
				item.x = SystemUtils.randRange(item.width*1.5,this.width-item.width*1.5);
				item.y = SystemUtils.randRange(item.height*1.5,this.height-item.height*1.5);
				item.rotation = Math.random()*(-30)+Math.random()*30;
//				item.rotation = Math.random()
			}
		}
	}
}